#pragma once
#include <iostream>
#include <string>
#include "Protocol.hpp"
enum
{
    Div_Err = 1,
    Mod_Err,
    Unknown,
};
class CalculatorComputer
{
public:
    Response CalculatorHelper(const Request &req)
    {
        Response resp;
        switch (req.op_)
        {
        case '+':
            resp.result_ = req.x_ + req.y_;
            break;
        case '-':
            resp.result_ = req.x_ - req.y_;
            break;
        case '*':
            resp.result_ = req.x_ * req.y_;
            break;
        case '/':
        {
            if (req.y_ == 0)
            {
                resp.code_ = Div_Err;
                resp.result_ = 0;
            }
            else
            {
                resp.code_ = 0;
                resp.result_ = req.x_ / req.y_;
            }
        }
        break;
        case '%':
        {
            if (req.y_ == 0)
            {
                resp.code_ = Mod_Err;
                resp.result_ = 0;
            }
            else
            {
                resp.code_ = 0;
                resp.result_ = req.x_ % req.y_;
            }
        }
        break;
        default:
            resp.code_ = Unknown;
            resp.result_ = 0;
            break;
        }
    }
    std::string Calculator(std::string &message)
    {
        std::string req_str;
        Request req;
        bool r = DeCode(message, req_str);
        if(false == r) return "";

        r = req.DeSerialize(req_str);
        if(false == r) return "";
        printf("%d %c % d = ?\n", req.x_, req.op_, req.y_);
        Response resp = CalculatorHelper(req);

        std::string resp_body;
        r = resp.Serialize(resp_body);
        if(false == r) return "";
        
        std::string resp_str;
        resp_str = EnCode(resp_body);

        return resp_str;
        
    }
};